package com.customization.unionpay.market.workflowaction;

import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.maketEnum.InstructionEnum;
import com.customization.unionpay.market.util.FormDataUtils;
import weaver.generalledger.action.ZzgdExecuteInstrucitonAction;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.OverTimeSetBean;
import weaver.workflow.request.RequestManager;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author sjm
 * @description 营销个税计提单预算执行释放相关
 */
public class MarketingPersonalTaxLadingAction implements Action {

    private static final Logger log = LoggerFactory.getLogger(MarketingPersonalTaxLadingAction.class);

    @Override
    public String execute(RequestInfo requestInfo) {

        OverTimeSetBean overTimeBean = new OverTimeSetBean();

        RequestManager rm = requestInfo.getRequestManager();
        try {
            String src = rm.getSrc();
            log.info("src："+src);
            int nodeId = rm.getNodeid();

            Map<String, Object> allFormData = com.customization.unionpay.market.util.FormDataUtils.getAllFormData(requestInfo);

            Map<String, String> mainMap = (Map<String, String>) allFormData.get("mainMap");
            // 主 业务日期        ywrq
            String ywrq = mainMap.get("ywrq");
            log.info("ywrq："+ywrq);
            // 主 预算控制期      yskzq
            String yskzq = mainMap.get("yskzq");
            log.info("yskzq："+yskzq);
            String ywrqStr = com.customization.unionpay.market.util.FormDataUtils.changeDateString("yyyy-MM-dd", ywrq, "yyyy");
            String yskzqStr = FormDataUtils.changeDateString("yyyy-MM-dd", yskzq, "yyyy");

            if ("reject".equals(src)) {
                String instruction = "";
                if(ywrqStr.equals(yskzqStr)){
                    instruction = com.customization.unionpay.market.maketEnum.InstructionEnum.SFZY.toString();
                }else {
                    instruction = InstructionEnum.XMSFZY.toString();
                }
                log.info("instruction："+instruction);
                ZzgdExecuteInstrucitonAction zzgdExecuteInstrucitonAction = new ZzgdExecuteInstrucitonAction();
                zzgdExecuteInstrucitonAction.setZl(instruction);
                String execute = zzgdExecuteInstrucitonAction.execute(requestInfo);
                if("0".equals(execute)){
                    String messageid = requestInfo.getRequestManager().getMessageid();
                    log.info("messageid："+messageid);
                    String messagecontent = requestInfo.getRequestManager().getMessagecontent();
                    log.info("messagecontent："+messagecontent);
                    return Action.FAILURE_AND_CONTINUE;
                }else {
                    log.info("预算调用成功！");
                }
            } else {
                String nodeName = overTimeBean.getNodeName(nodeId);
                log.info("nodeName："+nodeName);
            }
        } catch (Exception e) {
            // 异常报错是填写异常信息，阻止流程继续流转
            requestInfo.getRequestManager().setMessageid("10001");
            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }

    /**
     * 拼接预算参数到中间表
     * @param requestName
     * @param yskzq
     * @param yskzqStr
     * @param mainMap
     * @param detailMap
     * @param bdh           被执行单号
     * @param requestId     执行单号
     * @param tableName
     * @param id
     * @return
     * @throws Exception
     */
    public JSONObject getParamsAddMiddle(String requestName,String yskzq,String yskzqStr,Map<String, String> mainMap,Map<String, Map<String, Map<String, String>>> detailMap,String bdh,String requestId,String tableName,String id) throws Exception {

        ResourceComInfo rci = new ResourceComInfo();
        DepartmentComInfo dci = new DepartmentComInfo();
        SubCompanyComInfo scci = new SubCompanyComInfo();

        JSONObject param = new JSONObject();

        String xmbh = mainMap.get("project_code");
        log.info("xmbh："+xmbh);
        String sqr = mainMap.get("sqr");

        String workcode = rci.getWorkcode(sqr);
        String lastname = rci.getLastname(sqr);

        String bzbm = mainMap.get("bzbm");

        String departmentCode = dci.getDepartmentCode(bzbm);
        String departmentname = dci.getDepartmentname(bzbm);

        String bzgs = mainMap.get("bzgs");

        String subCompanyName = scci.getSubCompanyname(bzgs);
        String subCompanyCode = scci.getSubCompanyCode(bzgs);

        String bzsy = mainMap.get("bzsy");

        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());


        Map<String, Map<String, String>> dt3 = detailMap.get("dt3");

        for (String key : dt3.keySet()) {
            Map<String, String> value = dt3.get(key);
            String jjsx = value.get("jjsx");
            log.info("jjsx:"+jjsx);
//            JSONObject economicMattersInfo = FormDataUtils.getEconomicMattersInfoByCode(jjsx);
//            log.info("economicMattersInfo:"+economicMattersInfo);
            // 内部出资金额
            String jtgsje = "".equals(value.get("jtgsje")) ? "0" : (value.get("jtgsje") == null ? "0" : value.get("jtgsje"));
            String yszrzx = value.get("yszrzx");
            String yskm = value.get("yskm");

            // 主 项目编号                 ysxm                预算项目
            param.put("ysxm",xmbh);
            // 明细  预算科目               yskm	             预算科目
            param.put("yskm",yskm);
            // 明细  预算责任中心           zhdw	             执行单位
            param.put("zhdw",yszrzx);
            //                           ysqjn               预算期间（年）	      必传，预算入账期间加工成4位年份
            param.put("ysqjn",yskzqStr);
            // 明细                      je                   金额
            param.put("je",jtgsje);
            // 主 表单号                  bzydjh               被占用单据号	     非必传，下列情况必传： 释放占用且执行,释放执行且占用、释放占用（必传）, 占用（特殊情况必传）
            param.put("bzydjh",bdh);
            // 主 表单号                  zhdjh                执行单据号        必传
            param.put("zhdjh",requestId);
            // 主 预算控制期               yskzq              预算控制期           必传，预算的入账期间
            param.put("yskzq",yskzq);
            //                           zhrq	            执行日期	           必传，当前日期
            param.put("zhrq",format);
            //                           zhdjlx	            执行单据类型	        必传
            param.put("zhdjlx","营销个税计提流程");
            // 主 申请人                   bzr                  报账人          必传，工号-姓名
            param.put("bzr",workcode+"-"+lastname);
            //                           lyxt	            来源系统	           必传，智慧财务平台
            param.put("lyxt","智慧财务平台");
            // 主 报账事由                 sm	                 说明  	        必传，各报账单的主表备注字段
            param.put("sm",bzsy);
            // 主 报账部门                 tdrbm                报账部门        必传，部门编码-部门名称
            param.put("tdrbm",departmentCode+"-"+departmentname);
            // 主 报账公司                 szgs                 报账公司        必传，公司编码-公司名称
            param.put("szgs",subCompanyCode+"-"+subCompanyName);
            // 明细  经济事项              jjsx                  经济事项             必传，经济事项编号
            param.put("jjsx",jjsx);
            // 明细  事项
            param.put("event","");
            // 明细  来源id
            param.put("lylc",id);
            // 明细  来源业务表名
            param.put("lyywbm",tableName);
        }

        return param;
    }
}
